﻿using System;
using System.Windows.Forms;
using HuaCheng.CommonClass;
using HuaCheng.DataAccess;

namespace HuaCheng.Views
{
    /// <summary>
    /// 更新密码
    /// </summary>
    public partial class UpdatePwd : Form
    {
        public UpdatePwd()
        {
            InitializeComponent();
            tbUsername.Enabled = false;
            tbUsername.Text = IndexPage.username;
        }

        /// <summary>
        /// 退出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void confirmBtn_Click(object sender, EventArgs e)
        {
            if (tbPwd.Text.Trim() == "")
            {
                MessageBox.Show("旧密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (tbNewpwd.Text.Trim() == "")
            {
                MessageBox.Show("新密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (tbConfirmpwd.Text.Trim() == "")
            {
                MessageBox.Show("确认新密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (tbPwd.Text != UserInfoHelper.RetrieveUserinfo(tbUsername.Text).PassWord)
            {
                MessageBox.Show("密码错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (tbNewpwd.Text.Trim() != tbConfirmpwd.Text.Trim())
            {
                MessageBox.Show("两次输入的新密码不一致，请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                UserInfo userinfo = new UserInfo()
                {
                    UserName = tbUsername.Text,
                    PassWord = tbNewpwd.Text
                };
                UserInfoHelper.UpdatePwd(userinfo);
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
            }
        }
        #region 国际化
        private void UpdatePwd_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("UpdatePwdText");
            this.lblUsername.Text = ResourceCulture.GetString("UPUserNameText");
            this.lblPwd.Text = ResourceCulture.GetString("OldPwdText");
            this.lblNewpwd.Text = ResourceCulture.GetString("NewPwdText");
            this.lblConfirmpwd.Text = ResourceCulture.GetString("ConfirmPwdText");
            this.btnConfirm.Text = ResourceCulture.GetString("UpdatePwdOk");
            this.btnCancel.Text = ResourceCulture.GetString("UpdatePwdCancel");
        }
        #endregion   
    }
}
